# Code for Table H3: Randomization Test with Alternative Fixed Effect Specifications

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------
require('AER')
require('ivpack')
require('dplyr')
require('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))


## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)


## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

## ---------------------------------------
# Construct alternative fixed effects
# bail hearing year-day-of-the-week, month-day- of-the-week, and day-of-the-week-shift, and case severity tercile indicators
## ---------------------------------------
data0812[, court_time_1D := .GRP, by = c("court_time1", "court_dow")]
data0812[, court_time_2D := .GRP, by = c("court_time2", "court_dow")]
data0812[, court_time_3D := .GRP, by = c("court_dow", "court_shift")]

## Reorder levels to match table
data0812$race <- factor(data0812$race, levels = c("Black", "White", "Other", "Unknown"))


## ---------------------------------------
## Column 1
ss3a <- ivreg(pti ~ age_2012 + I(age_2012^2) + Female + as.factor(race) 
              + as.factor(any_drug_2) +  as.factor(any_violent_2)  
              + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) 
              + as.factor(prior_offender_2)
              + vote2008 + as.factor(noteli08) + regis_before
              + as.factor(court_time_1D) + as.factor(court_time_2D) + as.factor(court_time_3D)  + as.factor(totOGS2),
              data = data0812)

ss3a0 <- ivreg(pti ~   as.factor(court_time_1D) + as.factor(court_time_2D) + as.factor(court_time_3D)  + as.factor(totOGS2), data = data0812)


## ---------------------------------------
## Column 2
ss3b <- lm(judgeiv ~ age_2012 + I(age_2012^2) + Female + as.factor(race) 
           + as.factor(any_drug_2) +  as.factor(any_violent_2)  
           + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) 
           + as.factor(prior_offender_2)
           + vote2008 + as.factor(noteli08) + regis_before
           + as.factor(court_time_1D) + as.factor(court_time_2D) + as.factor(court_time_3D)  + as.factor(totOGS2)
           , data = data0812)

ss3b0 <- lm(judgeiv ~
              as.factor(court_time_1D) + as.factor(court_time_2D) + as.factor(court_time_3D)  + as.factor(totOGS2)
            , data = data0812)


t1 <- cbind(round(robust.se(ss3a)[, 1], 5), round(robust.se(ss3b)[, 1], 5))
## Std Errors
t2 <- cbind(round(robust.se(ss3a)[, 2], 5), round(robust.se(ss3b)[, 2], 5))

keep <- c("age_2012",
          "I(age_2012^2)",
          "Female",
          "as.factor(race)White",
          "as.factor(race)Other",
          "as.factor(race)Unknown",
          "as.factor(any_drug_2)1",
          "as.factor(any_violent_2)1",
          "as.factor(fire_arms_2)1",
          "as.factor(any_rob_2)1",
          "as.factor(any_dui_2)1",
          "as.factor(prior_offender_2)1",
          "vote2008",
          "as.factor(noteli08)1",
          "regis_before")

cat("\nTable H3: Randomization Test Alternative Fixed Effects\n")
## Coeffs
coefs <- as.data.frame(t1[rownames(t1) %in% keep, ])
names(coefs) <- c("Pretrial Incarceration Est.", "Magistrate Leniency Est.")
cat("\nPrinting Table H3 estimates...\n")
print(coefs)

## Std Errors
se <- as.data.frame(t2[rownames(t2) %in% keep, ])
names(se) <- c("Pretrial Incarceration SE", "Magistrate Leniency SE")
cat("\nPrinting Table H3 standard errors...\n")
print(se)

## Joint F-test
cat("\nPrinting Table H3, Joint F-Test Pretrial Incarceration...\n")
print(round((anova(ss3a, ss3a0)$F)[2],2))
cat("\nPrinting Table H3, Joint F-Test Magistrate Leniency...\n")
print(round((anova(ss3b, ss3b0)$F)[2], 3))


